	
	/******************************************************************************\
	|    Title:        	 Replication code for Globalization, Government            |
	|                    Popularity, and the Great Skill Divide     			   |
	|    Date:         	 September 2023                                            |
	|	 Description:    Gallup data cleaning and merging      					   |
	\******************************************************************************/

*******************************************************************************
**#            	   I -  Globals and variable definitions 
*******************************************************************************

	* Dropbox folder
	use "$conf/raw/Gallup_raw.dta", clear

	drop region*
	*drop region* index*
	
	rename year year_1
	rename year_calendar year
	
	rename countrynew country_gallup
		
		
	replace country_gallup="Bosnia and Herzegovina" if country_gallup=="Bosnia Herzegovina"
	replace country_gallup="Macedonia, FYR" if country_gallup=="Macedonia"
	replace country_gallup="Slovak Republic" if country_gallup=="Slovakia"
	replace country_gallup="Somalia" if country_gallup=="Somaliland"
	replace country_gallup="Maldives" if country_gallup=="Maldives 9"

		
	gen stata_id=. 	
	replace stata_id=1 		if country_gallup=="Afghanistan"
	replace stata_id=2	 	if country_gallup=="Albania"
	replace stata_id=3		if country_gallup=="Algeria"
	replace stata_id=4		if country_gallup=="Angola"
	replace stata_id=5		if country_gallup=="Argentina"
	replace stata_id=6		if country_gallup=="Armenia"
	replace stata_id=7		if country_gallup=="Australia"
	replace stata_id=8		if country_gallup=="Austria"
	replace stata_id=9		if country_gallup=="Azerbaijan"
	replace stata_id=10		if country_gallup=="Bahamas, The" | country_gallup=="Bahamas"
	replace stata_id=11		if country_gallup=="Bahrain"
	replace stata_id=12		if country_gallup=="Bangladesh"
	replace stata_id=13		if country_gallup=="Barbados"
	replace stata_id=14		if country_gallup=="Belarus"
	replace stata_id=15		if country_gallup=="Belgium"
	replace stata_id=16		if country_gallup=="Belize"
	replace stata_id=17		if country_gallup=="Benin"
	replace stata_id=18		if country_gallup=="Bhutan"
	replace stata_id=19		if country_gallup=="Bolivia" | country_gallup=="Bolivia (Plurinational State of)"
	replace stata_id=21		if country_gallup=="Bosnia and Herzegovina" | country_gallup=="Bosnia Herzegovina"
	replace stata_id=22		if country_gallup=="Botswana"
	replace stata_id=23		if country_gallup=="Brazil"
	replace stata_id=24		if country_gallup=="Brunei Darussalam"
	replace stata_id=25		if country_gallup=="Bulgaria"
	replace stata_id=26		if country_gallup=="Burkina Faso"
	replace stata_id=27		if country_gallup=="Burundi"
	replace stata_id=28		if country_gallup=="Cabo Verde"
	replace stata_id=29		if country_gallup=="Cambodia"
	replace stata_id=30		if country_gallup=="Cameroon"
	replace stata_id=31		if country_gallup=="Canada"
	replace stata_id=32		if country_gallup=="Cape Verde"
	replace stata_id=33		if country_gallup=="Central African Republic" | country_gallup=="Central African Rep."
	replace stata_id=34		if country_gallup=="Chad"
	replace stata_id=35		if country_gallup=="Chile"
	replace stata_id=36		if country_gallup=="China" 
	replace stata_id=37		if country_gallup=="Colombia"
	replace stata_id=38		if country_gallup=="Comoros"
	replace stata_id=39		if country_gallup=="Congo Brazzaville" | country_gallup=="Congo"
	replace stata_id=40		if country_gallup=="Congo Kinshasa" | country_gallup=="Democratic Republic of the Congo"
	replace stata_id=41		if country_gallup=="Costa Rica"
	replace stata_id=42		if country_gallup=="Croatia"
	replace stata_id=43		if country_gallup=="Cuba"
	replace stata_id=44		if country_gallup=="Cyprus"
	replace stata_id=45		if country_gallup=="Czech Republic" |  country_gallup=="Czechia"
	replace stata_id=46		if country_gallup=="Denmark"
	replace stata_id=47		if country_gallup=="Djibouti"
	replace stata_id=48		if country_gallup=="Dominican Republic" | country_gallup=="Dominican Rep."
	replace stata_id=49		if country_gallup=="East Timor"
	replace stata_id=50		if country_gallup=="Ecuador"
	replace stata_id=51		if country_gallup=="Egypt"| country_gallup=="Egypt, Arab Rep."
	replace stata_id=52		if country_gallup=="El Salvador"
	replace stata_id=53		if country_gallup=="Equatorial Guinea"
	replace stata_id=54		if country_gallup=="Eritrea"
	replace stata_id=55		if country_gallup=="Estonia"
	replace stata_id=56		if country_gallup=="Ethiopia"
	replace stata_id=58		if country_gallup=="Fiji"
	replace stata_id=59		if country_gallup=="Finland"
	replace stata_id=60		if country_gallup=="France"
	replace stata_id=61		if country_gallup=="Gabon"
	replace stata_id=62		if country_gallup=="Gambia" | country_gallup=="Gambia, The"
	replace stata_id=63		if country_gallup=="Georgia"
	replace stata_id=64		if country_gallup=="Germany"
	replace stata_id=65		if country_gallup=="Ghana"
	replace stata_id=66		if country_gallup=="Greece"
	replace stata_id=67		if country_gallup=="Guatemala"
	replace stata_id=68		if country_gallup=="Guinea" | country_gallup=="Guinea-Bissau"
	replace stata_id=70		if country_gallup=="Guyana"
	replace stata_id=71		if country_gallup=="Haiti"
	replace stata_id=72		if country_gallup=="Honduras"
	replace stata_id=73		if country_gallup=="Hong Kong" | country_gallup=="China, Hong Kong SAR"
	replace stata_id=75		if country_gallup=="Hungary"
	replace stata_id=76		if country_gallup=="Iceland"
	replace stata_id=77		if country_gallup=="India"
	replace stata_id=78		if country_gallup=="Indonesia"
	replace stata_id=79		if country_gallup=="Iran" | country_gallup=="Iran (Islamic Republic of)"
	replace stata_id=80		if country_gallup=="Iraq"
	replace stata_id=81		if country_gallup=="Ireland"
	replace stata_id=82		if country_gallup=="Israel"
	replace stata_id=83		if country_gallup=="Italy"
	replace stata_id=84		if country_gallup=="Ivory Coast" | country_gallup=="C𴥠d'Ivoire"
	replace stata_id=85		if country_gallup=="Jamaica"
	replace stata_id=86		if country_gallup=="Japan"
	replace stata_id=87		if country_gallup=="Jordan"
	replace stata_id=88		if country_gallup=="Kazakhstan"
	replace stata_id=89		if country_gallup=="Kenya"
	replace stata_id=91		if country_gallup=="Korea North" | country_gallup=="Dem. People's Republic of Korea"
	replace stata_id=92		if country_gallup=="Kosovo"
	replace stata_id=93		if country_gallup=="Kuwait"
	replace stata_id=95		if country_gallup=="Kyrgyzstan" | country_gallup=="Kyrgyz Republic"
	replace stata_id=96		if country_gallup=="Laos" | country_gallup=="Lao People's Democratic Republic"
	replace stata_id=97		if country_gallup=="Latvia"
	replace stata_id=98		if country_gallup=="Lebanon"
	replace stata_id=99		if country_gallup=="Lesotho"
	replace stata_id=100	if country_gallup=="Liberia"
	replace stata_id=101	if country_gallup=="Libya"
	replace stata_id=102	if country_gallup=="Lithuania"
	replace stata_id=103	if country_gallup=="Luxembourg"
	replace stata_id=104	if country_gallup=="Macao SAR" | country_gallup=="China, Macao SAR"
	replace stata_id=106	if country_gallup=="Macedonia, FYR" | country_gallup=="TFYR Macedonia" | country_gallup=="TFYR of Macedonia" | country_gallup == "Macedonia"
	replace stata_id=107	if country_gallup=="Madagascar"
	replace stata_id=108	if country_gallup=="Malawi"
	replace stata_id=109	if country_gallup=="Malaysia"
	replace stata_id=110	if country_gallup=="Mali"
	replace stata_id=111	if country_gallup=="Malta"
	replace stata_id=112	if country_gallup=="Mauritania"
	replace stata_id=113	if country_gallup=="Mauritius"
	replace stata_id=114	if country_gallup=="Mexico"
	replace stata_id=115	if country_gallup=="Moldova" | country_gallup=="Rep. of Moldova"
	replace stata_id=116	if country_gallup=="Mongolia"
	replace stata_id=117	if country_gallup=="Montenegro"
	replace stata_id=118	if country_gallup=="Morocco"
	replace stata_id=119	if country_gallup=="Mozambique"
	replace stata_id=120	if country_gallup=="Myanmar"
	replace stata_id=122	if country_gallup=="Nagorno Karabakh"
	replace stata_id=123	if country_gallup=="Namibia"
	replace stata_id=124	if country_gallup=="Nepal"
	replace stata_id=125	if country_gallup=="Netherlands"
	replace stata_id=126	if country_gallup=="New Zealand"
	replace stata_id=127	if country_gallup=="Nicaragua"
	replace stata_id=128	if country_gallup=="Niger"
	replace stata_id=129	if country_gallup=="Nigeria"
	replace stata_id=130	if country_gallup=="Northern Cyprus"
	replace stata_id=131	if country_gallup=="Norway"
	replace stata_id=132	if country_gallup=="Oman"
	replace stata_id=133	if country_gallup=="Pakistan"
	replace stata_id=134	if country_gallup=="Palestine" | country_gallup=="State of Palestine"
	replace stata_id=135	if country_gallup=="Panama"
	replace stata_id=136	if country_gallup=="Papua New Guinea"
	replace stata_id=137	if country_gallup=="Paraguay"
	replace stata_id=138	if country_gallup=="Peru"
	replace stata_id=139	if country_gallup=="Philippines"
	replace stata_id=140	if country_gallup=="Poland"
	replace stata_id=141	if country_gallup=="Portugal"
	replace stata_id=142	if country_gallup=="Puerto Rico"
	replace stata_id=143	if country_gallup=="Qatar"
	replace stata_id=144	if country_gallup=="Romania"
	replace stata_id=145	if country_gallup=="Russia" | country_gallup=="Russian Federation"
	replace stata_id=146	if country_gallup=="Rwanda"
	replace stata_id=147	if country_gallup=="San Marino"
	replace stata_id=148	if country_gallup=="Saudi Arabia"
	replace stata_id=149	if country_gallup=="Senegal"
	replace stata_id=150	if country_gallup=="Serbia"
	replace stata_id=151	if country_gallup=="Serbia and Montenegro"
	replace stata_id=152	if country_gallup=="Seychelles"
	replace stata_id=153	if country_gallup=="Sierra Leone"
	replace stata_id=154	if country_gallup=="Singapore"
	replace stata_id=155	if country_gallup=="Slovak Republic" | country_gallup=="Slovakia"
	replace stata_id=157	if country_gallup=="Slovenia"
	replace stata_id=158	if country_gallup=="Solomon Islands"
	replace stata_id=159	if country_gallup=="Somalia"
	replace stata_id=161	if country_gallup=="South Africa"
	replace stata_id=162	if country_gallup=="South Korea" | country_gallup=="Rep. of Korea" | country_gallup=="Republic of Korea"
	replace stata_id=163	if country_gallup=="South Sudan"
	replace stata_id=164	if country_gallup=="Spain"
	replace stata_id=165	if country_gallup=="Sri Lanka"
	replace stata_id=166	if country_gallup=="Sudan"
	replace stata_id=168	if country_gallup=="Suriname"
	replace stata_id=169	if country_gallup=="Swaziland"
	replace stata_id=170	if country_gallup=="Sweden"
	replace stata_id=171	if country_gallup=="Switzerland"
	replace stata_id=172	if country_gallup=="Syria" | country_gallup=="Syrian Arab Republic"
	replace stata_id=173	if country_gallup=="Sao Tome Principe" | country_gallup=="Sao Tome and Principe"
	replace stata_id=174	if country_gallup=="Taiwan"
	replace stata_id=175	if country_gallup=="Taiwan Province of China"
	replace stata_id=176	if country_gallup=="Tajikistan"
	replace stata_id=177	if country_gallup=="Tanzania" | country_gallup=="United Rep. of Tanzania"
	replace stata_id=178	if country_gallup=="Thailand"
	replace stata_id=179	if country_gallup=="Togo"
	replace stata_id=180	if country_gallup=="Trinidad and Tobago"
	replace stata_id=181	if country_gallup=="Tunisia"
	replace stata_id=182	if country_gallup=="Turkey"
	replace stata_id=183	if country_gallup=="Turkmenistan"
	replace stata_id=184	if country_gallup=="UAE"
	replace stata_id=185	if country_gallup=="Uganda"
	replace stata_id=186	if country_gallup=="Ukraine"
	replace stata_id=187	if country_gallup=="United Arab Emirates"
	replace stata_id=188	if country_gallup=="United Kingdom" 
	replace stata_id=189	if country_gallup=="United States of America" | country_gallup=="USA" | country_gallup == "United States"
	replace stata_id=190	if country_gallup=="Uruguay"
	replace stata_id=191	if country_gallup=="Uzbekistan"
	replace stata_id=192	if country_gallup=="Venezuela" | country_gallup=="Venezuela (Bolivarian Republic of)"
	replace stata_id=193	if country_gallup=="Viet Nam" | country_gallup == "Vietnam"
	replace stata_id=194	if country_gallup=="Yemen" | country_gallup=="Yemen, Rep."
	replace stata_id=195	if country_gallup=="Zambia"
	replace stata_id=196	if country_gallup=="Zimbabwe"
	replace stata_id=197	if country_gallup=="Maldives"

	* Detect unmatched stata_id and country names

	tab country_gallup if stata_id==.

	tab stata_id

	drop if stata_id==.

	drop if wpid==7200100000297 // In 2012 we had an outlier value of $25, 073,129,949.85

	sort wpid
	* MERGE THE PRE-2009 INCOME VARIABLES
	merge 1:1 wpid using "$conf/income_pre2009.dta"
	drop if _merge==2 //LEFT JOIN
	drop income_1_old-income_7_old inc_004_old inc_013_old _merge

	* HH income in International Dollars

	replace income_2=inc_001_old if income_2==. & year<2009 //add measured hh income information for 2005-2008 period
	replace income_2=inc_002_old if income_2==. & year<2009 //add imputed hh income information for 2005-2008 period
	replace hhsize=headcount_old if hhsize==. & year<2009 //add hh size information for 2005-2008 period

	replace income_2=. if income_2==0 // Set "0" income to missing
	replace income_4=. if income_4==0 // Set "0" income to missing
	replace hhsize=. if hhsize==0 // Set "0" household size to missing

	* Per capita income in International Dollars

	gen pc_income2=income_2/hhsize //generate a new per capita income (ID) variable
	replace income_4=pc_income2 if income_4==.

	* Generate the log of income

	gen log_income_2=log(income_2+1)
	label var log_income_2 "The Log of HH Income"

	gen log_income_4=log(income_4+1)
	label var log_income_4 "The Log of Personal Income"

	* Age:
	rename wp1220 age
	label var age "Age"

	* Value 99 represents 99+, 100 is refused and 101 is weird
	recode age (100 101=.)
	gen age2=age*age
	label var age2 "Age-squared"
		
	* Partnered dummy:
	rename wp1223 mstatus
	label var mstatus "Marital status"
	recode mstatus (6 7=.)

	gen partnered = (mstatus==2) | (mstatus==8) if !missing(mstatus)
	label var partnered "Married/DP"	
		
	* Gender

	rename wp1219 gender
	label var gender "Gender"
	gen male=(gender==1)
	replace male=. if missing(gender)
	label var male "Male"	
		
	* Education
	rename wp3117 education
	recode education (4 5 = .) // Don't know and refused as missing
	//tab education


	* Urbanity Status

	gen urban=1 if (wp14==3 | wp14==6)
	replace urban=0 if (wp14==1 | wp14==2)
	//tab urban
	label var urban "Urban"

	gen rural=urban==0
	replace rural=. if missing(urban)
	label var rural "Rural"


	* New labels
	label var education "Education level (3 categories)"
	label define edu 1 "Primary or less (max 8 yrs)" 2 "Secondary (9 - 15 yrs)" 3 "Tertiary (4 yrs college degree)"
	label values education edu
	gen tertiary = (education == 3)
	replace tertiary = . if missing(education)
	gen less_ter = (tertiary == 0)
	replace less_ter = . if missing(tertiary)

		
	global demographics "male age age2 partnered"	
		
		
	gen missing1=0
		foreach var of varlist male age age2 partnered log_income_2 tertiary urban rural {
			replace missing1=1 if missing(`var')
		}
		tab missing1
		
	*	drop if missing1==1 
			

	************************ Generate low-income country indicator ($1,025 or less) ************************

	gen lowincome=stata_id==1				//Afghanistan
	replace lowincome=1 if stata_id==68 	//Guinea
	replace lowincome=1 if stata_id==146 	//Rwanda
	replace lowincome=1 if stata_id==17		//Benin
	replace lowincome=1 if stata_id==149	//Senegal
	replace lowincome=1 if stata_id==26 	//Burkina Faso
	replace lowincome=1 if stata_id==71		//Haiti
	replace lowincome=1 if stata_id==153	//Sierra Leone
	replace lowincome=1 if stata_id==27		//Burundi
	replace lowincome=1 if stata_id==91		//North Korea
	replace lowincome=1 if stata_id==159	//Somalia
	replace lowincome=1 if stata_id==33		//Central African Republic
	replace lowincome=1 if stata_id==100	//Liberia
	replace lowincome=1 if stata_id==163	//South Sudan
	replace lowincome=1 if stata_id==34		//Chad
	replace lowincome=1 if stata_id==107 	//Madagascar
	replace lowincome=1 if stata_id==177	//Tanzania
	replace lowincome=1 if stata_id==38		//Comoros
	replace lowincome=1 if stata_id==108 	//Malawi
	replace lowincome=1 if stata_id==179	//Togo
	replace lowincome=1 if stata_id==40		//Congo, Dem. Rep
	replace lowincome=1 if stata_id==110	//Mali
	replace lowincome=1 if stata_id==185 	//Uganda
	replace lowincome=1 if stata_id==54		//Eritrea
	replace lowincome=1 if stata_id==119	//Mozambique
	replace lowincome=1 if stata_id==196 	//Zimbabwe
	replace lowincome=1 if stata_id==56		//Ethiopia
	replace lowincome=1 if stata_id==124	//Nepal
	replace lowincome=1 if stata_id==62 	//Gambia
	replace lowincome=1 if stata_id==128	//The Niger
	//tab lowincome
	label var lowincome "Low-income country"

	************************ Generate lower-middle income country indicator ($1,026 to $4,035) ************************

	gen lowermiddleincome=stata_id==6				//Armenia
	replace lowermiddleincome=1 if stata_id==12 	//Bangladesh
	replace lowermiddleincome=1 if stata_id==18 	//Bhutan
	replace lowermiddleincome=1 if stata_id==19 	//Bolivia
	replace lowermiddleincome=1 if stata_id==28		//Cabo Verde
	replace lowermiddleincome=1 if stata_id==29		//Cambodia
	replace lowermiddleincome=1 if stata_id==30 	//Cameroon
	replace lowermiddleincome=1 if stata_id==39		//Congo, Rep.
	replace lowermiddleincome=1 if stata_id==84		//Côte d'Ivoire
	replace lowermiddleincome=1 if stata_id==47		//Djibouti
	replace lowermiddleincome=1 if stata_id==51		//Egypt
	replace lowermiddleincome=1 if stata_id==52		//El Salvador
	replace lowermiddleincome=1 if stata_id==65		//Ghana
	replace lowermiddleincome=1 if stata_id==67		//Guatemala
	replace lowermiddleincome=1 if stata_id==72		//Honduras
	replace lowermiddleincome=1 if stata_id==77		//India
	replace lowermiddleincome=1 if stata_id==78 	//Indonesia
	replace lowermiddleincome=1 if stata_id==89		//Kenya
	replace lowermiddleincome=1 if stata_id==92		//Kosovo
	replace lowermiddleincome=1 if stata_id==95 	//Kyrgyz Republic
	replace lowermiddleincome=1 if stata_id==96		//Lao PDR
	replace lowermiddleincome=1 if stata_id==99		//Lesotho
	replace lowermiddleincome=1 if stata_id==112	//Mauritania
	replace lowermiddleincome=1 if stata_id==115 	//Moldova
	replace lowermiddleincome=1 if stata_id==116	//Mongolia
	replace lowermiddleincome=1 if stata_id==118	//Morocco
	replace lowermiddleincome=1 if stata_id==120 	//Myanmar
	replace lowermiddleincome=1 if stata_id==127	//Nicaragua
	replace lowermiddleincome=1 if stata_id==129	//Nigeria
	replace lowermiddleincome=1 if stata_id==133 	//Pakistan
	replace lowermiddleincome=1 if stata_id==136	//Papua New Guinea
	replace lowermiddleincome=1 if stata_id==139 	//Philippines
	replace lowermiddleincome=1 if stata_id==173	//São Tomé and Principe
	replace lowermiddleincome=1 if stata_id==158	//Solomon Islands
	replace lowermiddleincome=1 if stata_id==165 	//Sri Lanka
	replace lowermiddleincome=1 if stata_id==166	//Sudan
	replace lowermiddleincome=1 if stata_id==169	//Swaziland
	replace lowermiddleincome=1 if stata_id==172	//Syrian Arab Republic
	replace lowermiddleincome=1 if stata_id==176	//Tajikistan
	replace lowermiddleincome=1 if stata_id==49		//Timor-Leste
	replace lowermiddleincome=1 if stata_id==181	//Tunisia
	replace lowermiddleincome=1 if stata_id==186	//Ukraine
	replace lowermiddleincome=1 if stata_id==191	//Uzbekistan
	replace lowermiddleincome=1 if stata_id==193	//Vietnam
	replace lowermiddleincome=1 if stata_id==134	//West Bank and Gaza
	replace lowermiddleincome=1 if stata_id==194 	//Yemen, Rep.
	replace lowermiddleincome=1 if stata_id==195	//Zambia
	//tab lowermiddleincome
	label var lowermiddleincome "Lower-middle income country"

	************************ Generate upper-middle income country indicator ($4,036 to $12,475) ************************

	gen uppermiddleincome=stata_id==2				//Albania
	replace uppermiddleincome=1 if stata_id==3		//Algeria
	replace uppermiddleincome=1 if stata_id==4 		//Angola
	replace uppermiddleincome=1 if stata_id==5		//Argentina
	replace uppermiddleincome=1 if stata_id==9		//Azerbaijan
	replace uppermiddleincome=1 if stata_id==14 	//Belarus
	replace uppermiddleincome=1 if stata_id==16		//Belize
	replace uppermiddleincome=1 if stata_id==21		//Bosnia and Herzegovina
	replace uppermiddleincome=1 if stata_id==22 	//Botswana
	replace uppermiddleincome=1 if stata_id==23		//Brazil
	replace uppermiddleincome=1 if stata_id==25		//Bulgaria
	replace uppermiddleincome=1 if stata_id==36		//China
	replace uppermiddleincome=1 if stata_id==37 	//Colombia
	replace uppermiddleincome=1 if stata_id==41		//Costa Rica
	replace uppermiddleincome=1 if stata_id==43		//Cuba
	replace uppermiddleincome=1 if stata_id==48		//Dominican Republic
	replace uppermiddleincome=1 if stata_id==53		//Equatorial Guinea
	replace uppermiddleincome=1 if stata_id==50 	//Ecuador
	replace uppermiddleincome=1 if stata_id==58		//Fiji
	replace uppermiddleincome=1 if stata_id==61		//Gabon
	replace uppermiddleincome=1 if stata_id==63		//Georgia
	replace uppermiddleincome=1 if stata_id==70		//Guyana
	replace uppermiddleincome=1 if stata_id==79		//Iran, Islamic Rep
	replace uppermiddleincome=1 if stata_id==80		//Iraq
	replace uppermiddleincome=1 if stata_id==85 	//Jamaica
	replace uppermiddleincome=1 if stata_id==87		//Jordan
	replace uppermiddleincome=1 if stata_id==88		//Kazakhstan
	replace uppermiddleincome=1 if stata_id==98 	//Lebanon
	replace uppermiddleincome=1 if stata_id==101	//Libya
	replace uppermiddleincome=1 if stata_id==106	//Macedonia, FYR
	replace uppermiddleincome=1 if stata_id==109	//Malaysia
	replace uppermiddleincome=1 if stata_id==113	//Mauritius
	replace uppermiddleincome=1 if stata_id==114	//Mexico
	replace uppermiddleincome=1 if stata_id==117 	//Montenegro
	replace uppermiddleincome=1 if stata_id==123	//Namibia
	replace uppermiddleincome=1 if stata_id==135 	//Panama
	replace uppermiddleincome=1 if stata_id==137	//Paraguay
	replace uppermiddleincome=1 if stata_id==138	//Peru
	replace uppermiddleincome=1 if stata_id==144	//Romania
	replace uppermiddleincome=1 if stata_id==145 	//Russian Federation
	replace uppermiddleincome=1 if stata_id==150	//Serbia
	replace uppermiddleincome=1 if stata_id==161	//South Africa
	replace uppermiddleincome=1 if stata_id==168	//Suriname
	replace uppermiddleincome=1 if stata_id==178 	//Thailand
	replace uppermiddleincome=1 if stata_id==182	//Turkey
	replace uppermiddleincome=1 if stata_id==183	//Turkmenistan
	replace uppermiddleincome=1 if stata_id==192 	//Venezuela, RB
	replace uppermiddleincome=1 if stata_id==197 	//Maldives
	//tab uppermiddleincome
	label var uppermiddleincome "Upper-middle income country"

	************************ Generate high income country indicator ($12,476 or more) ************************

	gen highincome=stata_id==7				//Australia
	replace highincome=1 if stata_id==8		//Austria
	replace highincome=1 if stata_id==10	//Bahamas, The
	replace highincome=1 if stata_id==11 	//Bahrain
	replace highincome=1 if stata_id==13	//Barbados
	replace highincome=1 if stata_id==15	//Belgium
	replace highincome=1 if stata_id==24	//Brunei Darussalam
	replace highincome=1 if stata_id==31	//Canada
	replace highincome=1 if stata_id==35	//Chile
	replace highincome=1 if stata_id==42	//Croatia
	replace highincome=1 if stata_id==44	//Cyprus
	replace highincome=1 if stata_id==45	//Czech Republic
	replace highincome=1 if stata_id==46 	//Denmark
	replace highincome=1 if stata_id==55 	//Estonia
	replace highincome=1 if stata_id==59 	//Finland
	replace highincome=1 if stata_id==60	//France
	replace highincome=1 if stata_id==64	//Germany
	replace highincome=1 if stata_id==66 	//Greece
	replace highincome=1 if stata_id==73 	//Hong Kong SAR, China
	replace highincome=1 if stata_id==75	//Hungary
	replace highincome=1 if stata_id==76	//Iceland
	replace highincome=1 if stata_id==81	//Ireland
	replace highincome=1 if stata_id==82	//Israel
	replace highincome=1 if stata_id==83	//Italy
	replace highincome=1 if stata_id==86 	//Japan
	replace highincome=1 if stata_id==162	//Korea, Rep.
	replace highincome=1 if stata_id==93 	//Kuwait
	replace highincome=1 if stata_id==97	//Latvia
	replace highincome=1 if stata_id==102	//Lithuania
	replace highincome=1 if stata_id==103 	//Luxembourg
	replace highincome=1 if stata_id==104	//Macao SAR, China
	replace highincome=1 if stata_id==111	//Malta
	replace highincome=1 if stata_id==125	//Netherlands
	replace highincome=1 if stata_id==126 	//New Zealand
	replace highincome=1 if stata_id==131	//Norway
	replace highincome=1 if stata_id==132	//Oman
	replace highincome=1 if stata_id==140	//Poland
	replace highincome=1 if stata_id==141	//Portugal
	replace highincome=1 if stata_id==142 	//Puerto Rico
	replace highincome=1 if stata_id==143	//Qatar
	replace highincome=1 if stata_id==147	//San Marino
	replace highincome=1 if stata_id==148 	//Saudi Arabia
	replace highincome=1 if stata_id==152	//Seychelles
	replace highincome=1 if stata_id==154	//Singapore
	replace highincome=1 if stata_id==155	//Slovak Republic
	replace highincome=1 if stata_id==157	//Slovenia
	replace highincome=1 if stata_id==164	//Spain
	replace highincome=1 if stata_id==170 	//Sweden
	replace highincome=1 if stata_id==171	//Switzerland
	replace highincome=1 if stata_id==174 	//Taiwan
	replace highincome=1 if stata_id==180	//Trinidad and Tobago
	replace highincome=1 if stata_id==187	//United Arab Emirates
	replace highincome=1 if stata_id==188 	//United Kingdom
	replace highincome=1 if stata_id==189	//United States
	replace highincome=1 if stata_id==190	//Uruguay
	//tab highincome
	label var highincome "High income country"
			

	gen Low_Income=lowincome
	tab Low_Income

	gen Lower_Middle_Income=lowermiddleincome
	tab Lower_Middle_Income

	gen Upper_Middle_Income=uppermiddleincome
	tab Upper_Middle_Income

	gen High_Income=highincome
	tab High_Income

	gen High_UpperMiddle_Income=highincome | uppermiddleincome
	tab High_UpperMiddle_Income

	gen Low_LowerMiddle_Income=High_UpperMiddle_Income==0
	tab Low_LowerMiddle_Income

	gen Middle_Income=Lower_Middle_Income==1 | Upper_Middle_Income==1
	tab Middle_Income		

	* Income groups:
	bysort year: egen inc_33 = pctile(income_4), p(33.3)
	label var inc_33 "Poorest %33.3"

	bysort year: egen inc_66 = pctile(income_4), p(66.6)
	label var inc_66 "Second %33.3"

	gen inc_terciles=.
	replace inc_terciles=1 if !missing(income_4) & income_4 <= inc_33
	replace inc_terciles=2 if !missing(income_4) & income_4 > inc_33 & income_4<=inc_66 
	replace inc_terciles=3 if !missing(income_4) & income_4 > inc_66 

	tab inc_terciles, g(income_tercile_)

	rename income_tercile_1 Lower_inc
	label var Lower_inc "Lower income group"

	rename income_tercile_2 Middle_inc
	label var Middle_inc "Lower income group"

	rename income_tercile_3 High_inc
	label var High_inc "High income group"
		
		
		* Merge with newly created IVs and trade data:
	merge m:1 stata_id year using "$data/2002_2018_Exports_withIV.dta"
	tab country_gallup if _merge == 1	
	drop if _merge == 2
	drop if _merge == 1
	drop _merge
	tab year

	merge m:1 stata_id year using "$data/2002_2018_Imports_withIV.dta"
	keep if _merge == 3
	drop _merge
		
	********************************************************************************	

		** Generate trade variables and interactions:
	gen high_export =  highskilled_exports + mediumskilled_exports
	gen low_export = lowskilled_exports + labourintensive_exports

	foreach var of varlist high_export low_export {
		gen l_`var' = log(`var')
	}
		label var l_high_export "Log High Skill Exports"	
		label var l_low_export "Log Low Skill Exports"	
		label var Dif_l_high_export_1 "Δ Log Predicted High Skill Exports"	
		label var Dif_l_low_export_1 "Δ Log Predicted Low Skill Exports"	
		
	gen high_import =  highskilled_imports + mediumskilled_imports
	gen low_import = lowskilled_imports + labourintensive_imports

	foreach var of varlist high_import low_import {
		gen l_`var' = log(`var')
	}
		label var l_high_import "Log High Skill Imports"	
		label var l_low_import "Log Low Skill Imports"	
		label var Dif_l_high_import_1 "Δ Log High Skill Imports"	
		label var Dif_l_low_import_1 "Δ Log Low Skill Imports"	
		
	foreach var of varlist l_high_import l_high_export {
		gen tertiary_`var' = `var'*tertiary
	}
		label var tertiary_l_high_import "Tertiary*Log High Skill Imports"
		label var tertiary_l_high_export "Tertiary*Log High Skill Exports"

		foreach var of varlist l_low_import l_low_export {
		gen less_ter_`var' = `var'*less_ter
	}	
		label var less_ter_l_low_import "Less tertiary*Log Low Skill Imports"
		label var less_ter_l_low_export "Less tertiary*Log Low Skill Exports"
		
		** Predicted trade variables:
	gen high_export_IV =  IV_export_1_pair_highskilled + IV_export_1_pair_mediumskilled
	gen low_export_IV = IV_export_1_pair_lowskilled + IV_export_1_pair_labourintensive

	foreach var of varlist high_export_IV low_export_IV {
		gen l_`var' = log(`var')
	}
		label var l_high_export_IV "Log Predicted High Skill Exports"	
		label var l_low_export_IV "Log Predicted Low Skill Exports"	
		label var Dif_l_high_export_IV_1 "Δ Log Predicted High Skill Exports"	
		label var Dif_l_low_export_IV_1 "Δ Log Predicted Low Skill Exports"	
		
	gen high_import_IV =  IV_import_1_pair_highskilled + IV_import_1_pair_mediumskilled
	gen low_import_IV = IV_import_1_pair_lowskilled + IV_import_1_pair_labourintensive

	foreach var of varlist high_import_IV low_import_IV {
		gen l_`var' = log(`var')
	}
		label var l_high_import_IV "Log Predicted High Skill Imports"	
		label var l_low_import_IV "Log Predicted Low Skill Imports"	
		label var Dif_l_high_import_IV_1 "Δ Log Predicted High Skill Imports"	
		label var Dif_l_low_import_IV_1 "Δ Log Predicted Low Skill Imports"	

	foreach var of varlist l_high_import_IV l_high_export_IV {
		gen tertiary_`var' = `var'*tertiary
	}
		label var tertiary_l_high_import_IV "Tertiary*Log Predicted High Skill Imports"
		label var tertiary_l_high_export_IV "Tertiary*Log Predicted High Skill Exports"

		foreach var of varlist l_low_import_IV l_low_export_IV {
		gen less_ter_`var' = `var'*less_ter
	}	
		label var less_ter_l_low_import_IV "Less tertiary*Log Predicted Low Skill Imports"
		label var less_ter_l_low_export_IV "Less tertiary*Log Predicted Low Skill Exports"
		
		label var tertiary "Tertiary"
		label var less_ter "Less than tertiary"
		
		
		foreach var of varlist low_import_IV low_export_IV high_export_IV high_import_IV{
			replace missing1 = 1 if missing(`var')
		}
			tab missing1
	*		drop if missing1 == 1
			

	* Generate total export:
	gen l_export = log(high_export+low_export)
	gen l_import = log(high_import+low_import)

	label var l_export "Log Total Exports"
	label var l_import "Log Total Imports"

	gen tert_l_export = tertiary*l_export
	gen tert_l_import = tertiary*l_import

	label var tert_l_export "Tertiary Education$\times$ Log Total Exports"
	label var tert_l_import "Tertiary Education$\times$ Log Total Imports"
			
	** External data:
	* Country level polity score "polity2"
	merge m:1 country_gallup year using "$data/Polity_cleaned.dta"
	drop if _merge==2
	gen merge_polity=_merge
	drop _merge
			
			
		**** Outcomes:
	gen approvaloftheleader=wp150
	replace approvaloftheleader=0 if approvaloftheleader==2
	replace approvaloftheleader=. if approvaloftheleader==3 | approvaloftheleader==4
	tab approvaloftheleader
	label var approvaloftheleader "Approval of leadership of the country"

	rename wp139 confidence_government
	rename confidence_government confidence_government_original
	gen confidence_government = 1 if confidence_government_original == 1
	replace confidence_government = 0 if confidence_government_original == 2
	label var confidence_government "Have confidence in national government"

	global outcomes "confidence_government approvaloftheleader"
			
			replace missing1 = 1 if missing(confidence_government) & missing(approvaloftheleader)
			tab missing1
	*		drop if missing1 == 1

	** Placebos:
	rename wp137 confidence_military
	label var confidence_military "Have confidence in military"
	rename wp138 confidence_courts
	label var confidence_courts "Have confidence in courts"
	rename wp144 confidence_elections
	label var confidence_elections "Have confidence in honesty of elections"
	rename wp143 confidence_media
	label var confidence_media "Have confidence in Media"


	recode confidence_military confidence_courts confidence_elections confidence_media (2 = 0) (3 4 = .)

	* Satisfied:
	gen satisfied = wp16
	label var satisfied "Life satisfaction"
	replace satisfied=0 if satisfied==0 | satisfied==1 | satisfied==2 | satisfied==3 | satisfied==4 | satisfied==5
	replace satisfied=1 if satisfied==6 | satisfied==7 | satisfied==8 | satisfied==9 | satisfied==10
	replace satisfied=. if satisfied==98 | satisfied==99
	tab satisfied

	* Alternative life satisfaction measure
	gen satisfied_1 = wp16
	label var satisfied_1 "Life satisfaction"
	replace satisfied_1=0 if satisfied_1==0 | satisfied_1==1 | satisfied_1==2 | satisfied_1==3 | satisfied_1==4 | satisfied_1==5 | satisfied_1==6
	replace satisfied_1=1 if satisfied_1==7 | satisfied_1==8 | satisfied_1==9 | satisfied_1==10
	replace satisfied_1=. if satisfied_1==98 | satisfied_1==99
	tab satisfied_1

	* Law:
	rename wp112 law_confidenceinpolice
	label var law_confidenceinpolice "Have confidence in the local police force"
	tab law_confidenceinpolice
	rename wp113 law_safewalking
	label var law_safewalking "Safe walking alone at night in the city or area"
	tab law_safewalking
	rename wp117 law_stolen
	label var law_stolen "Had money or property stolen"
	tab law_stolen

	foreach var of varlist law_confidenceinpolice law_safewalking law_stolen {
		recode `var' (2 = 0) (3 4 = .)
	}

	** Optimism:
	recode wp16 wp18 (98 99 = .)
	gen optimism_fiveyears = (wp18 > wp16) if !missing(wp18) & !missing(wp16)
	label var optimism_fiveyears "Expect oneself to have higher satisfaction in five years"
	tab optimism_fiveyears

	* Satisfaction:
	rename wp91 community_transportation
	label var community_transportation "Satisfied with the public transportation systems"
	tab community_transportation
	rename wp92 community_roads
	label var community_roads "Satisfied with the roads"
	tab community_roads
	rename wp93 community_education
	label var community_education "Satisfied with the educational system"
	tab community_education
	rename wp94 community_air
	label var community_air "Satisfied with the quality of air"
	tab community_air
	rename wp95 community_water
	label var community_water "Satisfied with the water"
	tab community_water
	rename wp97 community_healthcare
	label var community_healthcare "Satisfied with the availability of quality healthcare"
	tab community_healthcare
	rename wp98 community_housing
	label var community_housing "Satisfied with the availability of good affordable housing"
	tab community_housing

	foreach var of varlist community_transportation community_roads community_education community_air community_water community_healthcare community_housing {
		recode `var' (2 = 0) (3 4 = .)
	}
		
	* Income terciles:

	bysort year country_gallup: egen inc_ctry_d10 = pctile(income_4), p(10)
	label var inc_ctry_d10 "10% of income (per Country-Year)"

	bysort year country_gallup: egen inc_ctry_d20 = pctile(income_4), p(20)
	label var inc_ctry_d20 "20% of income (per Country-Year)"

	bysort year country_gallup: egen inc_ctry_d30 = pctile(income_4), p(30)
	label var inc_ctry_d30 "30% of income (per Country-Year)"

	bysort year country_gallup: egen inc_ctry_d40 = pctile(income_4), p(40)
	label var inc_ctry_d40 "40% of income (per Country-Year)"

	bysort year country_gallup: egen inc_ctry_d50 = pctile(income_4), p(50)
	label var inc_ctry_d50 "50% of income (per Country-Year)"

	bysort year country_gallup: egen inc_ctry_d60 = pctile(income_4), p(60)
	label var inc_ctry_d60 "60% of income (per Country-Year)"

	bysort year country_gallup: egen inc_ctry_d70 = pctile(income_4), p(70)
	label var inc_ctry_d70 "70% of income (per Country-Year)"

	bysort year country_gallup: egen inc_ctry_d80 = pctile(income_4), p(80)
	label var inc_ctry_d80 "80% of income (per Country-Year)"

	bysort year country_gallup: egen inc_ctry_d90 = pctile(income_4), p(90)
	label var inc_ctry_d90 "90% of income (per Country-Year)"
	gen inc_decile_ctry=.

	replace inc_decile_ctry=1 if !missing(income_4) & income_4 <= inc_ctry_d10
	replace inc_decile_ctry=2 if !missing(income_4) & income_4 > inc_ctry_d10 & income_4<=inc_ctry_d20
	replace inc_decile_ctry=3 if !missing(income_4) & income_4 > inc_ctry_d20 & income_4<=inc_ctry_d30
	replace inc_decile_ctry=4 if !missing(income_4) & income_4 > inc_ctry_d30 & income_4<=inc_ctry_d40
	replace inc_decile_ctry=5 if !missing(income_4) & income_4 > inc_ctry_d40 & income_4<=inc_ctry_d50
	replace inc_decile_ctry=6 if !missing(income_4) & income_4 > inc_ctry_d50 & income_4<=inc_ctry_d60
	replace inc_decile_ctry=7 if !missing(income_4) & income_4 > inc_ctry_d60 & income_4<=inc_ctry_d70
	replace inc_decile_ctry=8 if !missing(income_4) & income_4 > inc_ctry_d70 & income_4<=inc_ctry_d80
	replace inc_decile_ctry=9 if !missing(income_4) & income_4 > inc_ctry_d80 & income_4<=inc_ctry_d90
	replace inc_decile_ctry=10 if !missing(income_4) & income_4 > inc_ctry_d90

	tab inc_decile_ctry, g(inc_dec_ctry_)

	label var inc_dec_ctry_1 "First decile within Country-Year"
	label var inc_dec_ctry_2 "Second decile within Country-Year"
	label var inc_dec_ctry_3 "Third decile within Country-Year"
	label var inc_dec_ctry_4 "Fourth decile within Country-Year"
	label var inc_dec_ctry_5 "Fifth decile within Country-Year"
	label var inc_dec_ctry_6 "Sixth decile within Country-Year"
	label var inc_dec_ctry_7 "Seventh decile within Country-Year"
	label var inc_dec_ctry_8 "Eight decile within Country-Year"
	label var inc_dec_ctry_9 "Nineth decile within Country-Year"
	label var inc_dec_ctry_10 "Tenth decile within Country-Year"

	gen inc_tercile_ctry=0 if inc_decile_ctry==1|inc_decile_ctry==2|inc_decile_ctry==3
	replace inc_tercile_ctry=1 if inc_decile_ctry==4|inc_decile_ctry==5|inc_decile_ctry==6
	replace inc_tercile_ctry=2 if inc_decile_ctry==7|inc_decile_ctry==8|inc_decile_ctry==9|inc_decile_ctry==10

	* Country level gdp
	merge m:1 country_gallup year using "$data/GDPPC_cleaned.dta"
	drop if _merge==2
	gen merge_gdppc=_merge

	foreach var of varlist m30 wp2319 {
		di "`var'"
		tab `var'
		tab `var', nolabel
	}

	recode wp16 (98 99 . = .)
	rename wp16 life_today
	recode wp18 (98 99 . = .)
	rename wp18 life_5years
	recode wp23 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp23 health_problems
	recode wp30 (1 = 1) (2= 0) (3 4 . = .)
	rename wp30 standard_living
	recode wp31 (1 = 1) (2 3 = 0) (4 5 = .)
	rename wp31 standard_livingbetter
	recode wp40 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp40 notmoney_food
	recode wp67 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp67 experienced_enjoyment
	recode wp69 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp69 experienced_worry
	recode wp70 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp70 experienced_sadness
	recode wp71 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp71 experienced_stress
	recode wp74 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp74 experienced_anger
	recode wp89 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp89 local_jobmkt
	recode wp106 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp106 place_immigrants
	recode wp128 (1 = 1) (2 = 0) (3 4 . = .)
	rename wp128 hardwork
	recode wp148 (1 = 1) (2 3 = 0) (4 5 = .)
	rename wp148 nat_economy_better
	recode m30 (8 9 11 = .)
	rename m30 econ_conditions
	recode wp2319 (1 2 = 1) (3 4 = 0) (5 6 . = .)
	rename wp2319 feeling_hhincome


	* Life evaluation index:
	tab index_le
	recode index_le (1 = 3) (2 = 2) (3 = 1) (. = .), g(life_index)

	* Drop some variables to save memory:
	drop wp*
	
	* Save this version:
	save "$conf/Gallup_cleaned.dta", replace